﻿using System.Collections.Generic;
using Missbot.Common;
using Missbot.Core.Base;
using Missbot.Core.Cache;
using Dolany.UtilityTool;
using JetBrains.Annotations;
using Missbot.AIModule.Base;
using Missbot.AIModule.OnlineStore;
using Missbot.AIModule.Pet.Expedition;
using Missbot.Model.MsgInfos;

namespace Missbot.AIModule.Pet.PetAgainst
{
    [UsedImplicitly]
    public class PetAgainstMod : IAIModule
    {
        public override string Name => "宠物对决";
        public override string ParentTag => "宠物功能";

        #region Svcs

        public PetSkillSvc PetSkillSvc { get; set; }
        public WaiterSvc WaiterSvc { get; set; }
        public PetAgainstSvc PetAgainstSvc { get; set; }

        #endregion

        [ModuleEnter(Command = "查看宠物技能", Description = "查看指定的宠物技能详细情况", SyntaxHint = "[技能名]", ChannelType = ChannelType.Messaging)]
        public bool ViewPetSkill(InboxSession session, string name)
        {
            var skill = PetSkillSvc[name];
            if (skill == null)
            {
                session.SendBack("未查找到该技能！");
                return false;
            }

            var pet = PetRecordDA.Get(session.User.ID);
            var buffer = new OutputBuffer(session);
            buffer.AddPair(new Dictionary<string, string>
            {
                ["名称"] = skill.Name,
                ["描述"] = skill.CommDesc(),
                ["解锁"] = skill.LearnLevel.ToString(),
                ["当前"] = pet.Skills != null && pet.Skills.ContainsKey(skill.Name) ? pet.Skills[skill.Name].ToString() : "0"
            });

            buffer.Send();
            return true;
        }

        [ModuleEnter(Command = "升级宠物技能",
                     Description = "将指定的宠物技能等级提升一点（只能升级已经学会的技能）（最高5级）",
                     SyntaxHint = "[技能名]",
                     ChannelType = ChannelType.Messaging)]
        public bool UpgradePetSkill(InboxSession session, string name)
        {
            var skill = PetSkillSvc[name];
            if (skill == null)
            {
                session.SendBack("未查找到该技能！");
                return false;
            }

            var pet = PetRecordDA.Get(session.User.ID);
            var expRec = ExpeditionRecordDA.GetLastest(session.User.ID);
            if (expRec != null && expRec.IsExpediting())
            {
                session.SendBack($"**{pet.Name}**正在【{expRec.Scene}】进行一项伟大的远征，请于{expRec.EndTime:yyyy-MM-dd HH:mm:ss}后再试！");
                return false;
            }

            if (pet.Skills.IsNullOrEmpty() || !pet.Skills.ContainsKey(name))
            {
                session.SendBack($"**{pet.Name}**尚未学习该技能！");
                return false;
            }

            if (pet.Skills[name] >= 5)
            {
                session.SendBack($"**{pet.Name}**的该技能已经升到了满级！");
                return false;
            }

            if (pet.RemainSkillPoints <= 0)
            {
                session.SendBack($"**{pet.Name}**没有可用的技能点！");
                return false;
            }

            pet.Skills[name]++;
            pet.RemainSkillPoints--;
            pet.Update();

            session.SendBack($"恭喜**{pet.Name}**的{name}技能成功升到了{pet.Skills[name]}级！");
            return true;
        }

        [ModuleEnter(Command = "重置宠物技能", Description = "重置宠物的所有技能加点", SyntaxHint = "[技能名]", ChannelType = ChannelType.Messaging)]
        public bool ResetPetSkill(InboxSession session)
        {
            const int ResetSkillCost = 100;
            var osPerson = OSPersonDA.GetPerson(session.User.ID);
            if (osPerson.Golds < ResetSkillCost)
            {
                session.SendBack($"金币余额不足({osPerson.Golds.CurencyFormat()}/{ResetSkillCost.CurencyFormat()})");
                return false;
            }

            if (!WaiterSvc.WaitForConfirm_Gold(session, ResetSkillCost, 10))
            {
                session.SendBack("操作取消！");
                return false;
            }

            var pet = PetRecordDA.Get(session.User.ID);
            var expRec = ExpeditionRecordDA.GetLastest(session.User.ID);
            if (expRec != null && expRec.IsExpediting())
            {
                session.SendBack($"**{pet.Name}**正在【{expRec.Scene}】进行一项伟大的远征，请于{expRec.EndTime:yyyy-MM-dd HH:mm:ss}后再试！");
                return false;
            }

            pet.SkillReset();
            pet.Update();

            osPerson.Golds -= ResetSkillCost;
            osPerson.Update();

            session.SendBack("重置成功！");
            return true;
        }

        [ModuleEnter(Command = "宠物对决",
                     Description = "邀请指定群员进行宠物对决",
                     DailyLimit = 1,
                     SyntaxHint = "[@用户名]",
                     ChannelType = ChannelType.Messaging)]
        public bool Fight(InboxSession session, InstanceUser aimUser)
        {
            if (aimUser.ID == session.User.ID)
            {
                session.SendBack("你无法挑战你自己！");
                return false;
            }

            if (!PetAgainstSvc.CheckGroup(session.Group.ID))
            {
                session.SendBack("本群正在进行一场宠物对决，请稍后再试！");
                return false;
            }

            if (!PetAgainstSvc.CheckUser(session.User.ID))
            {
                session.SendBack("你的宠物正在进行一场宠物对决，请稍后再试！");
                return false;
            }

            if (!PetAgainstSvc.CheckUser(aimUser.ID))
            {
                session.SendBack("你的对手正在进行一场宠物对决，请稍后再试！");
                return false;
            }

            if (aimUser.IsBot)
            {
                session.SendBack("鱼唇的人类，你无法挑战AI的威严！");
                return false;
            }

            var sourcePet = PetRecordDA.Get(session.User.ID);
            var expRec = ExpeditionRecordDA.GetLastest(session.User.ID);
            if (expRec != null && expRec.IsExpediting())
            {
                session.SendBack($"**{sourcePet.Name}**正在【{expRec.Scene}】进行一项伟大的远征，请于{expRec.EndTime:yyyy-MM-dd HH:mm:ss}后再试！");
                return false;
            }

            if (sourcePet.Level < 3)
            {
                session.SendBack($"**{sourcePet.Name}**还没到3级，无法参加宠物对决！");
                return false;
            }

            var aimPet = PetRecordDA.Get(aimUser.ID);
            if (aimPet.Level < 3)
            {
                session.SendBack("对方的宠物还没到3级，无法参加宠物对决！");
                return false;
            }

            expRec = ExpeditionRecordDA.GetLastest(aimUser.ID);
            if (expRec != null && expRec.IsExpediting())
            {
                session.SendBack($"**{aimPet.Name}**正在【{expRec.Scene}】进行一项伟大的远征，请于{expRec.EndTime:yyyy-MM-dd HH:mm:ss}后再试！");
                return false;
            }

            if (!WaiterSvc.WaitForConfirm(new InboxSession
            {
                Group = session.Group,
                User = aimUser,
                Channel = session.Channel
            }, "你被邀请参加一场宠物对决，是否同意？", 10))
            {
                session.SendBack("对决取消！");
                return false;
            }

            PetAgainstSvc.StartGame(session.User, aimUser, session.Group);
            return true;
        }
    }
}